package com.lyp.springboot.service.impl;

import com.lyp.springboot.dao.UserDao;
import com.lyp.springboot.model.TestUser;
import com.lyp.springboot.service.UserService;
import com.lyp.springboot.utils.Md5;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

@Component
public class UserServiceImpl implements UserService {
    private static Log LOG = LogFactory.getLog(UserServiceImpl.class);
    @Autowired
    UserDao userDao;
    @Override
    public int  checkLoginUser(String name,String password){
        LOG.info("num:"+userDao.checkLoginUser(name));
        try{
            if(Md5.EncoderByMd5(password).equals(userDao.checkLoginUser(name))){
                return 1;
            }
        }catch (Exception e){
            return 0;
        }
        return 0;
    }
    @Override
    public void saveUser(String name, String realName, String password, String status, String creator){
        TestUser testUser = new TestUser();
        try {
            password = Md5.EncoderByMd5(password);
        }catch (Exception e){
            e.printStackTrace();
        }
        testUser.setName(name);
        testUser.setRealName(realName);
        testUser.setPassword(password);
        testUser.setCreateTime(new Date());
        testUser.setCreator("lyp");
        testUser.setStatus("1");
        userDao.saveUser(testUser);
    }

}
